﻿/**********************************************
系统:Ebada物流企业ERP
模块:示例
作者:Rabbit
创建时间:2011-2-28
最后一次修改:2011-2-28
***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform;
using System.Collections;

namespace Ebada.Modules.Document
{

    public partial class UCDocumentsRight : DevExpress.XtraEditors.XtraUserControl {
        private GridViewOperation<Documents> gridViewOperation;
        private IList<Documents> list;

        public IList<Documents> List
        {
            get { return list; }
            set {
                list = value;
                gridControl1.DataSource = list;
            }
        }
        public event SendDataEventHandler<Documents> FocusedRowChanged;
        private string parentID;
        private int opertype = 0;
        private string act_code = "";
        private Documents row;
        frmDocumentsEdit f = new frmDocumentsEdit();
        public string Act_code
        {
            get { return act_code; }
            set {
                act_code = value;
                string str = " where Act_Code='" + act_code + "' ";
                if (act_code != "")
                {
                    list = MainHelper.GetSqlMap<Documents>().GetListByWhere<Documents>(str);
                    gridControl1.DataSource = list;
                }
            }
        }


        public int Opertype
        {
            get { return opertype; }
            set { 
                opertype = value;
                if (value == 1)//提交预警
                {
                    string str = " where 1>1";
                    if (value!=0)
                    {
                        str = " where DocumentTypeID='" + parentID + "' and DocState=0 ";
                    }
                    list= MainHelper.GetSqlMap<Documents>().GetListByWhere<Documents>(str);
                    ArrayList badlist = new ArrayList();
                    for (int i = 0; i < list.Count;i++ )
                    {
                        Documents doc = list[i];
                        DocumentType dtype = MainHelper.GetSqlMap<DocumentType>().GetOneByKey<DocumentType>(doc.DocumentTypeID);
                        if (DateTime.Now < doc.SubmitDateLimit.AddDays(-Convert.ToDouble(dtype.SubmitDate)))
                        {
                            badlist.Add(doc);
                        }
                    }
                    for (int j = 0; j < badlist.Count; j++)
                    {
                        list.Remove((Documents)badlist[j]);
                    }
                    gridControl1.DataSource = list;
                }
                if (value == 2)//有效期预警
                {
                    string str = " where 1>1";
                    if (value != 0)
                    {
                        str = " where DocumentTypeID='" + parentID + "' and  DocState<>0 ";
                    }
                    list = MainHelper.GetSqlMap<Documents>().GetListByWhere<Documents>(str);
                    ArrayList badlist = new ArrayList();
                    for (int i = 0; i < list.Count; i++)
                    {
                        Documents doc = list[i];
                        if(DateTime.Now<doc.RemindDate)
                        {
                            badlist.Add(doc);
                        }
                    }
                    for (int j = 0; j < badlist.Count; j++)
                    {
                        list.Remove((Documents)badlist[j]);
                    }
                    gridControl1.DataSource = list;
                }
                hidebutton();
            }
        }

        public UCDocumentsRight() {
            InitializeComponent();
            initImageList();
            gridViewOperation = new GridViewOperation<Documents>(gridControl1, gridView1, barManager1, f);
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridViewOperation.BeforeAdd += new ObjectOperationEventHandler<Documents>(gridViewOperation_BeforeAdd);
            gridViewOperation.AfterAdd += new ObjectEventHandler<Documents>(gridViewOperation_AfterAdd);
            gridViewOperation.BeforeEdit += new ObjectOperationEventHandler<Documents>(gridViewOperation_BeforeEdit);
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridView1_FocusedRowChanged);
            initColumns();
        }

        void gridViewOperation_BeforeEdit(object render, ObjectOperationEventArgs<Documents> e)
        {
            f.bttype = 0;
        }

    
        /// <summary>
        /// 设置隐藏列
        /// </summary>
        void initColumns() {
            for (int i = 0; i < 4; i++)
            {
                gridView1.Columns[i].Visible = false;
            }
            gridView1.Columns["OrgName"].Caption = "责任单位名称";
            gridView1.Columns["DocState"].Caption = "单证状态";
            gridView1.Columns["DocState"].Visible = false;
            gridView1.Columns["DocStateName"].Caption = "单证状态";
            gridView1.Columns["DocCode"].Caption = "单证编号";
            gridView1.Columns["Act_Date"].Caption = "作业时间";
            gridView1.Columns["SubmitDateLimit"].Caption = "单证提交时限";
            gridView1.Columns["SubmitDate"].Caption = "实际提交时间";
            gridView1.Columns["ValidityDate"].Caption = "有效截至期";
            gridView1.Columns["RemindDate"].Caption = "有效期预警时间";
            gridView1.Columns["CheckResult"].Caption = "审查结果";
            gridView1.Columns["CheckRemark"].Caption = "审查说明";
            gridView1.Columns["CheckUserID"].Caption = "审查人ID";
            gridView1.Columns["CheckUserName"].Caption = "审查人";
            gridView1.Columns["CheckedDate"].Caption = "审查时间";
            gridView1.Columns["Org_ID"].Caption = "承运商";
            gridView1.Columns["Org_ID"].Visible = false;
            gridView1.Columns["SpliteFlag"].Visible = false;
            gridView1.Columns["ScrapDate"].Caption = "报废时间";
            gridView1.Columns["DocBookID"].Caption = "册子编号";
            gridView1.Columns["DocBookName"].Caption = "册子名称";
            gridView1.Columns["DocBookSeq"].Caption = "册子里序列号";
            gridView1.Columns["CreateUserID"].Caption = "创建人ID";
            gridView1.Columns["LastEditUserID"].Caption = "修改人ID";
        }
       public void initColumns2()
        {
            gridView1.OptionsBehavior.AutoSelectAllInEditor = true;
            gridView1.OptionsBehavior.Editable = true;
            for (int i = 0; i < 4; i++)
            {
                gridView1.Columns[i].Visible = false;
            }
            for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                gridView1.Columns[i].OptionsColumn.ReadOnly = true;
                gridView1.Columns[i].OptionsColumn.AllowEdit = false;
            }
            gridView1.Columns["OrgName"].Caption = "责任单位名称";
            gridView1.Columns["DocState"].Caption = "单证状态";
            gridView1.Columns["DocState"].Visible = false;
            gridView1.Columns["DocStateName"].Caption = "单证状态";
            gridView1.Columns["DocCode"].Caption = "单证编号";
            gridView1.Columns["Act_Date"].Caption = "作业时间";
            gridView1.Columns["SubmitDateLimit"].Caption = "单证提交时限";
            gridView1.Columns["SubmitDate"].Caption = "实际提交时间";
            gridView1.Columns["ValidityDate"].Caption = "有效截至期";
            gridView1.Columns["RemindDate"].Caption = "有效期预警时间";
            gridView1.Columns["CheckResult"].Caption = "审查结果";
            gridView1.Columns["CheckRemark"].Caption = "审查说明";
            gridView1.Columns["CheckUserID"].Caption = "审查人ID";
            gridView1.Columns["CheckUserName"].Caption = "审查人";
            gridView1.Columns["CheckedDate"].Caption = "审查时间";
            gridView1.Columns["Org_ID"].Caption = "承运商";
            gridView1.Columns["SpliteFlag"].Visible = false;
            gridView1.Columns["ScrapDate"].Caption = "报废时间";
            gridView1.Columns["DocBookID"].Caption = "册子编号";
            gridView1.Columns["DocBookID"].Visible = false;
            gridView1.Columns["DocBookName"].Caption = "册子名称";
            gridView1.Columns["DocBookName"].Visible = false;
            gridView1.Columns["DocBookSeq"].Caption = "册子里序列号";
            gridView1.Columns["DocBookSeq"].VisibleIndex = 0;
            gridView1.Columns["DocBookSeq"].OptionsColumn.AllowEdit = true;
            gridView1.Columns["DocBookSeq"].OptionsColumn.ReadOnly = false;
            gridView1.Columns["CreateUserID"].Caption = "创建人ID";
            gridView1.Columns["LastEditUserID"].Caption = "修改人ID";
        }
        public void hidebutton()
        {
            this.btAdd.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btClose.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btEdit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btExport.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.btDelete.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            

        }
        public void hidebutton2()
        {
            bar1.Visible = false;
        }
        void gridViewOperation_BeforeAdd(object render, ObjectOperationEventArgs<Documents> e) {
            if (string.IsNullOrEmpty(parentID)) {
                e.Cancel = true;
                MainHelper.ShowWarningMessageBox("请先选择分类后再增加单证！");
            }
            if(act_code!=""){
                e.Value.Act_Code = act_code;
            }
        }
        void gridViewOperation_AfterAdd(object render)
        {
            //添加到单证历史中
            DocumentHistory dh = new DocumentHistory();
            dh.DocumentHisID = dh.CreateID();
            FuncLib.documentWritedocumenthistory(ref dh, render as Documents);
            MainHelper.PlatformSqlMap.Create<DocumentHistory>(dh);
        }
        private void initImageList() {
            ImageList imagelist = new ImageList();
            imagelist.ImageStream = (Ebada.Client.Resource.UCGridToolbar.UCGridToolbarImageList);
            barManager1.Images = imagelist;
        }
        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) {
            if (FocusedRowChanged != null)
            {
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as Documents);
               
            }
            row = gridView1.GetFocusedRow() as Documents;
            if(row!=null)
            {
                if (row.DocState == 3)
                {
                    this.btEdit.Enabled = false;
                    this.barButtonItem2.Enabled = false;
                    this.barButtonItem3.Enabled = false;
                }
                else if (row.DocState == 2)
                {
                    this.btEdit.Enabled = false;
                    this.barButtonItem2.Enabled = false;
                    this.barButtonItem3.Enabled = false;
                }
                else if (row.DocState == 1)
                {
                    this.btEdit.Enabled = false;
                    this.barButtonItem2.Enabled = true;
                    this.barButtonItem3.Enabled = false;
                }
                else
                {
                    this.btEdit.Enabled = true;
                    this.barButtonItem2.Enabled = true;
                    this.barButtonItem3.Enabled = true;
                }
            }
           
        }
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        [Browsable(false)]
        public GridViewOperation<Documents> GridViewOperation {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(Documents newobj) {
           // newobj.DocumentID = Guid.NewGuid().ToString();
            newobj.DocumentTypeID = parentID;
           
        }
        /// <summary>
        /// 父表ID
        /// </summary>
        [Browsable(false)]
        public string ParentID {
            get { return parentID; }
            set {
                parentID = value;
                string str=" where 1>1";
                if(!string.IsNullOrEmpty(value)){
                    str = string.Format("where DocumentTypeID='{0}' order by DocState,CreatedDate", value);
                }
                gridViewOperation.RefreshData(str);
            }
        }

        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                string type = view.GetRowCellDisplayText(e.RowHandle, view.Columns["DocState"]);
                if (type == "0")
                {
                    //e.Appearance.BackColor = Color.White;
                }
                if (type=="1")
                {
                    e.Appearance.BackColor = Color.LightSkyBlue;
                }
                if (type == "2")
                {
                    e.Appearance.BackColor = Color.LightSeaGreen;
                }
                if (type == "3")
                {
                    e.Appearance.BackColor = Color.LightGray;
                }
            }

        }

        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {

        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //提交
            if (row == null)
            {
                MessageBox.Show("请选择记录。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            f.bttype = 1;
            f.RowData = row;
            if (f.ShowDialog() == DialogResult.OK)
            {
                MainHelper.GetSqlMap<DocumentType>().Update<Documents>(f.RowData as Documents);
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //审核
            if (row == null)
            {
                MessageBox.Show("请选择记录。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (row.DocState != 1)
            {
                MessageBox.Show("只能审核已提交的单证。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            f.bttype = 2;
            f.RowData = row;
            if (f.ShowDialog() == DialogResult.OK)
            {
                MainHelper.GetSqlMap<DocumentType>().Update<Documents>(f.RowData as Documents);
            }
            
        }

        private void btAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            frmDocumentbookEdit frm = new frmDocumentbookEdit();
            int[] n= gridView1.GetSelectedRows();
            IList<Documents> _doclist=new List<Documents>();
            for(int i=0;i<n.Length;i++){
                Documents doc = gridView1.GetRow(n[i]) as Documents;
                if (doc.DocBookID != "" || doc.DocBookName != "")
                {
                    MessageBox.Show("选择的第"+Convert.ToString(i+1)+"条单证已经被其他册子关联，请重新选择。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                doc.DocBookSeq = i + 1;
                _doclist.Add(doc);
            }
            frm.list=_doclist;
            frm.ShowDialog();
        }
    }
}
